package com.qinghe.utils.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * 日期时间格式化、转换、通用工具类.
 * 
 * @author longfei
 */
public class DateUtil {

	/**
	 * 格式化当前时间，并返回String
	 * 
	 * @param  pattern the pattern of the string
	 * 
	 * @return the formatted date-time string
	 * @author longfei
	 */
	public String format(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date());
	}

	/**
	 * 格式化时间，并返回String
	 * 
	 * @param  pattern the pattern of the string
	 * @param  date    Date
	 * 
	 * @return the formatted date-time string
	 * @author longfei
	 */
	public String format(String pattern, Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 格式化时间，并返回Date
	 * 
	 * @param  pattern    the pattern of the string
	 * @param  dateString the string to be parsed
	 * 
	 * @return A Date parsed from the string. In case of error, returns null.
	 * @author longfei
	 */
	public Date parse(String pattern, String dateString) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 一天前的日期
	 * 
	 * @param  date
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getDayBeforeDate(Date date) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DATE, -1);
		return cl.getTime();
	}

	/**
	 * x天前的日期
	 * 
	 * @param  date
	 * @param  x 天数
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getDayBeforeDate(Date date, int x) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DATE, -x);
		return cl.getTime();
	}

	/**
	 * 一周前的日期
	 * 
	 * @param  date
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getWeekBeforeDate(Date date) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DATE, -7);
		return cl.getTime();
	}

	/**
	 * 一月前的日期
	 * 
	 * @param  date
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getMonthBeforeDate(Date date) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.MONTH, -1);
		return cl.getTime();
	}

	/**
	 * x月前的日期
	 * 
	 * @param  date
	 * @param  x 月数
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getMonthBeforeDate(Date date, int x) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.MONTH, -x);
		return cl.getTime();
	}

	/**
	 * 一天后的日期
	 * 
	 * @param  date
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getDayAfterDate(Date date) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DATE, 1);
		return cl.getTime();
	}

	/**
	 * x天后的日期
	 * 
	 * @param  date
	 * @param  x 天数
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getDayAfterDate(Date date, int x) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DATE, x);
		return cl.getTime();
	}

	/**
	 * 一周后的日期
	 * 
	 * @param  date
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getWeekAfterDate(Date date) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DATE, 7);
		return cl.getTime();
	}

	/**
	 * x月后的日期
	 * 
	 * @param  date
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getMonthAfterDate(Date date) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.MONTH, 1);
		return cl.getTime();
	}

	/**
	 * x月后的日期
	 * 
	 * @param  date
	 * @param  x 月数
	 * 
	 * @return Date
	 * @author longfei
	 */
	public Date getMonthAfterDate(Date date, int x) {
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.MONTH, x);
		return cl.getTime();
	}

	/**
	 * 将小时：分：秒的个数的时间转化为秒
	 * 
	 * @param hourTime
	 * 
	 * @return
	 */
	public long getSecondsByTime(String hourTime) {
		long seconds = 0;
		if (StringUtils.isEmpty(hourTime)) {
			return seconds;
		}
		String[] times = hourTime.split(":");
		String hour = times[0];
		String min = times[1];
		if (times.length > 2) {
			String sec = times[2];
			seconds = Integer.valueOf(sec);
		}
		seconds = seconds + (Integer.valueOf(min) * 60);
		seconds = seconds + (Integer.valueOf(hour) * 60 * 60);

		return seconds;
	}

	/**
	 * 获取当前日期是星期几
	 * 
	 * @param date
	 * 
	 * @return 当前日期是星期几(0星期日，1星期一，2星期二，3星期三，4星期四，5星期五，6星期六)
	 */
	public long getDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return w;
	}

	public static void main(String[] args) {
		System.out.println(new DateUtil().getDayOfWeek(new Date()));
	}

}
